package com.david.client.ui;

import java.util.HashMap;

import com.david.client.ui.usermanager.DeleteUserForm;
import com.david.client.ui.usermanager.NewUserForm;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.widgets.QuickTips;
import com.gwtext.client.widgets.form.Field;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.LayoutRegionConfig;

public class MainPanel extends Composite {
	final private CentralFrame centralFrame = new CentralFrame();

	final private NaviTreePanel naviTree = new NaviTreePanel();

	public MainPanel() {
		Field.setMsgTarget("side");
		QuickTips.init();
		// create the main layout
		BorderLayout layout = createBorderLayout();

		// add a header
		layout.add(LayoutRegionConfig.NORTH, new TitlePanel());

		// setup the main / center panel
		layout.add(LayoutRegionConfig.CENTER, centralFrame);
		naviTree.setMainPanel(this);
		layout.add(LayoutRegionConfig.WEST, naviTree);
		initWidget(layout);
	}

	// private static String getScreenName(TreeNode node, String name) {
	// TreeNode parentNode = (TreeNode) node.getParentNode();
	// return (parentNode == null || parentNode.getParentNode() == null) ? name
	// : getScreenName(parentNode, parentNode.getText() + ">" + name);
	// }

	private BorderLayout createBorderLayout() {
		LayoutRegionConfig north = new LayoutRegionConfig();
		north.setSplit(false);
		north.setInitialSize(40);
		north.setTitlebar(false);
		north.setAutoScroll(false);

		LayoutRegionConfig west = new LayoutRegionConfig();
		west.setSplit(true);
		west.setInitialSize(300);
		west.setMinSize(175);
		west.setMaxSize(400);
		west.setTitlebar(true);
		west.setCollapsible(true);
		west.setAnimate(true);
		west.setCollapsed(false);
		west.setAutoScroll(false);

		LayoutRegionConfig east = new LayoutRegionConfig();
		east.setSplit(true);
		east.setInitialSize(202);
		east.setMinSize(175);
		east.setMaxSize(400);
		east.setTitlebar(true);
		east.setCollapsible(true);
		east.setAnimate(true);
		east.setAutoScroll(false);

		LayoutRegionConfig south = new LayoutRegionConfig();
		south.setSplit(true);
		south.setInitialSize(100);
		south.setMinSize(100);
		south.setMaxSize(200);
		south.setTitlebar(true);
		south.setCollapsible(true);
		south.setAnimate(true);
		south.setCollapsed(true);
		south.setAutoScroll(false);

		LayoutRegionConfig center = new LayoutRegionConfig();
		center.setTitlebar(false);
		center.setAutoScroll(true);
		center.setTabPosition("top");

		return new BorderLayout("100%", "100%", north, null, west, null, center);
	}

	public void showPanel(String panelName) {
		Widget widget = (Widget) screens.get(panelName);
		centralFrame.setWidget(widget);
	}

	private static native String getMessageHtml(String title, String message) /*-{
	 return ['<div class="msg">',
	 '<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
	 '<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc"><h3>', title, '</h3>', message, '</div></div></div>',
	 '<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
	 '</div>'].join('');
	 }-*/;

	private static HashMap screens = new HashMap();

	{
		screens.put("addUser", new NewUserForm());
		screens.put("delUser", new DeleteUserForm());
	}

}
